package com.cljz.makepassenger.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cljz.makepassenger.R;


/**
 * Created by Hankkin on 2015/1/7.
 * 弹窗类
 */
public class PubPopupWindow extends PopupWindow implements View.OnClickListener {

    private TextView            //弹窗从相册选择按钮
            item_popupwindows_cancel, select_man,
            select_woman,select_secret;           //弹窗取消按钮
    private View menuview;
    private SexSelect sexSelect;

    /**
     * 上传图片*************************
     *
     * @param context
     * @param
     */
    public PubPopupWindow(Activity context, SexSelect sexSelect) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        menuview = inflater.inflate(R.layout.pop_sex_select, null);
        this.sexSelect = sexSelect;
        item_popupwindows_cancel = (TextView) menuview.findViewById(R.id.sex_select_btn_cancel);   //取消按钮
        select_man = (TextView) menuview.findViewById(R.id.sex_select_btn_man);
        select_woman = (TextView) menuview.findViewById(R.id.sex_select_btn_woman);
        select_secret = (TextView) menuview.findViewById(R.id.sex_select_btn_secret);

        select_man.setOnClickListener(this);
        select_woman.setOnClickListener(this);
        select_secret.setOnClickListener(this);

        item_popupwindows_cancel.setOnClickListener(this);


        //设置SelectPicPopupWindow的View
        this.setContentView(menuview);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        //修改高度显示，解决被手机底部虚拟键挡住的问题  by黄海杰 at:2015-4-30
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x55000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //menuview添加ontouchlistener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        menuview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int height = menuview.findViewById(R.id.item_popu_alert_pare).getTop();
                int y = (int) motionEvent.getY();
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                    if (y > height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sex_select_btn_cancel:
                dismiss();
                break;
            case R.id.sex_select_btn_man:
                sexSelect.select(1);
                dismiss();
                break;
            case R.id.sex_select_btn_woman:
                sexSelect.select(2);
                dismiss();
                break;
            case R.id.sex_select_btn_secret:
                sexSelect.select(0);
                dismiss();
                break;
        }
    }

    public interface SexSelect {
        void select(int position);
    }
}
